<?php
class Admin_contact extends MY_Controller {
	public function __construct()
	{
		$this->data["menu"] = "contact";
		parent::__construct();		
		$this->data["base_link"] = base_index_admin . "contact/";
	}
	
	public function index()
	{	
		$pgintf = "";
		$listbody = "";
		$total_num = $this->contactModel->count_all();
		if($total_num > 0){
			list($pgintf, $startnum) = createPaging($_SERVER["PHP_SELF"], strip_tags(@$_GET["page"]), $total_num, $this->data["paging_value"], $_SERVER["QUERY_STRING"]);
			$i = 1;
			$result = $this->contactModel->get_all($this->data["paging_value"], $startnum, "contact.contact_id DESC");			
			foreach($result as $contact){
				
								
				$listbody .= "<tr>\n";
				$listbody .= "<td>{$contact->contact_id}</td>\n";		
				$listbody .= "<td>{$contact->name}</td>\n";				
				$listbody .= "<td>{$contact->email}</td>\n";				
				$listbody .= "<td><a href=\"".$this->data["base_link"]."view/{$contact->contact_id}\"><img src='".base_common_admin."images/ico_view.gif' /></a></td>\n";
				$listbody .= "<td><a href=\"javascript:void(0);\" onclick=\"delete_confirm({$contact->contact_id});\"><img src='".base_common_admin."images/ico_drop.gif' /></a></td>\n";
				$listbody .= "</tr>\n";
			}
		}else{
			$listbody .= "<tr>\n";
			$listbody .= "<td colspan='5'>".$this->data["lang"]["general"]["noresult"]."</td>\n";
			$listbody .= "</tr>\n";
		}
		
		$this->data["pgintf"] = $pgintf;
		$this->data["listbody"] = $listbody;
		@$this->data["msg"] = $_SESSION["msg"];
		unset($_SESSION["msg"]);
		
		$this->load->view('admin/contact/contact_list', $this->data);		
	}	
	
	public function view()
	{
		$contact_id = $this->uri->segment(4);
		$query = $this->contactModel->get_one($contact_id);
		if($query->num_rows < 1)
			header("Location: " . $this->data["base_link"]);
		$contact = $query->row_array();
		$query->free_result();
		extract($contact);
		
		$this->data["contact_id"] = $contact_id;
		$this->data["name"] = $name;
		$this->data["tel"] = $tel;
		$this->data["email"] = $email;
		$this->data["address"] = $address;
		$this->data["content"] = nl2br($content);
		
		$this->data["mail_title"] = "";
		$this->data["reply_to"] = "";
		$this->data["mail_content"] = "";
		
		$this->data["errorms_mail_title"] = "";
		$this->data["errorms_reply_to"] = "";
		$this->data["errorms_content"] = "";
		$this->data["chk_post"] = false;
		
		if(!empty($_POST)){
			$this->data["mail_title"] = $this->input->post("mail_title");
			$this->data["reply_to"] = $this->input->post("reply_to");
			$this->data["mail_content"] = $this->input->post("mail_content");
			
			$err_check = false;
			if(empty($this->data["mail_title"])){
				$this->data["errorms_mail_title"] = "<span class=\"required\"><br />" . $this->data["lang"]["contact"]["errorms_mail_title"] . "</span>";
				$err_check = true;
			}		
			/*	
			if(empty($this->data["reply_to"])){
				$this->data["errorms_reply_to"] = "<span class=\"required\"><br />" . $this->data["lang"]["contact"]["errorms_reply_to"] . "</span>";
				$err_check = true;
			}
			else{
				$regex = "/^([a-z0-9_\.-])+@(([a-z0-9_-])+\\.)+[a-z]{2,4}$/";		
				if(!preg_match($regex, $this->data["reply_to"])){
					$this->data["errorms_reply_to"] = "<span class=\"required\"><br />" . $this->data["lang"]["contact"]["errorms_reply_to2"] . "</span>";
					$err_check = true;
				}				
			}
			/**/
			if(empty($this->data["mail_content"])){
				$this->data["errorms_content"] = "<span class=\"required\"><br />" . $this->data["lang"]["contact"]["errorms_content"] . "</span>";
				$err_check = true;
			}
			if(!$err_check){
				$_SESSION["msg"] = "<p class=\"notice\">" . $this->data["lang"]["contact"]["mail_send_msg"] . "</p>";
				
				$mailsubject = $this->data["mail_title"];				
				$mailmessage = $this->data["mail_content"];
				
				
				require APPPATH . "/../common/phpmailer/class.phpmailer.php";

				//Create a new PHPMailer instance
				$mail = new PHPMailer();
				$mail->CharSet = 'UTF-8';
				//Tell PHPMailer to use SMTP
				$mail->IsSMTP();
				//Enable SMTP debugging
				// 0 = off (for production use)
				// 1 = client messages
				// 2 = client and server messages
				$mail->SMTPDebug  = 0;
				//Ask for HTML-friendly debug output
				$mail->Debugoutput = 'html';
				//Set the hostname of the mail server
				$mail->Host       = 'smtp.gmail.com';
				//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
				$mail->Port       = 587;
				//Set the encryption system to use - ssl (deprecated) or tls
				$mail->SMTPSecure = 'tls';
				//Whether to use SMTP authentication
				$mail->SMTPAuth   = true;
				//Username to use for SMTP authentication - use full email address for gmail
				$mail->Username   = $this->data["shopEmail"];
				//Password to use for SMTP authentication
				$mail->Password   = $this->data["shopEmailPass"];
				//Set who the message is to be sent from
				$mail->SetFrom($this->data["shopEmail"], $this->data["shopName"]);
				//Set an alternative reply-to address
				$mail->AddReplyTo($this->data["shopEmail"], $this->data["shopName"]);
				//Set who the message is to be sent to
				$mail->AddAddress($email, $name);
				//Set the subject line
				$mail->Subject = $mailsubject;
				//Read an HTML message body from an external file, convert referenced images to embedded, convert HTML into a basic plain-text alternative body
				$mail->MsgHTML($mailmessage);
				//Replace the plain text body with one created manually
				//$mail->AltBody = 'This is a plain-text message body';
				//Attach an image file
				//$mail->AddAttachment('images/phpmailer-mini.gif');
				
				//Send the message, check for errors
				if(!$mail->Send()) {
				  echo "Mailer Error: " . $mail->ErrorInfo;
				} else {
				  echo "Message sent!";
				}
			}
			else{
				$this->data["chk_post"] = true;
			}
		}
		
		@$this->data["msg"] = $_SESSION["msg"];
		
		unset($_SESSION["msg"]);
		
		$this->load->view('admin/contact/contact_view', $this->data);		
	}	
	
	function delete(){
		@$id = $_POST["id"];
		@$page = $_POST["page"];		
		if(!empty($id)){
			$this->contactModel->delete_entry($id);
			$pageView = "";
			if(!empty($page))
				$pageView = "?page=" . $page;
			$_SESSION["msg"] = "<p class=\"notice\">" . $this->data["lang"]["general"]["delete_msg"] . "</p>";
			header("Location: " . $this->data["base_link"] . $pageView);
		}
	}
}